package com.nbx.app.zeuscmdport;


import java.nio.FloatBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.opengl.GLU;
import android.opengl.GLUtils;
import android.view.MotionEvent;

/**
 * http://www.zeuscmd.com/tutorials/opengles/17-TextureMapping.php
 * 
 * @author 5109200610
 *
 */
public class Render23 extends RenderBase {

	private int mTextureID[];

	private float bgVertices[] = {
		 1.0f, -1.0f,
		 1.0f,  1.0f,
		-1.0f, -1.0f,
		-1.0f,  1.0f
	};

	private float bgTexCoords[] = {
	 	3.0f, 0.0f,
		3.0f, 3.0f,
		0.0f, 0.0f,
		0.0f, 3.0f
	};

	private float checkerCoords[] = {
		3.0f, 0.0f,
		3.0f, 3.0f,
		0.0f, 0.0f,
		0.0f, 3.0f
	};
	
	private float wrapValue = 0.0f;

	private FloatBuffer mBgVBuffer;
	
	private FloatBuffer mBgTBuffer;
	
	private FloatBuffer mCheckerBuffer;
	
	public Render23(Context context) {
		super(context);
	}

	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		loadTextures(gl);
		
		mBgVBuffer = createFloatBuffer(bgVertices); 
		mBgTBuffer = createFloatBuffer(bgTexCoords); 
		mCheckerBuffer = createFloatBuffer(checkerCoords); 

		gl.glEnable(GL10.GL_TEXTURE_2D);

		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glDepthFunc(GL10.GL_LEQUAL);

		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		gl.glClearDepthf(1.0f);

		gl.glEnable(GL10.GL_CULL_FACE);
		gl.glShadeModel(GL10.GL_SMOOTH);

		gl.glVertexPointer(2, GL10.GL_FLOAT, 0, mBgVBuffer);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
	}

	public void onSurfaceChanged(GL10 gl, int width, int height) {
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();

		gl.glViewport(0, 0, width, height);
		GLU.gluPerspective(gl, 45.0f, 1.0f * width / height, 1.0f, 100.0f);

		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
	}

	public void onDrawFrame(GL10 gl) {
		gl.glClear (GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		gl.glLoadIdentity();

		gl.glTranslatef(0.0f, 0.0f, -2.0f);

		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mBgTBuffer);

		gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID[0]);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

		gl.glEnable(GL10.GL_BLEND);
		gl.glDisable(GL10.GL_DEPTH_TEST);

		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, mCheckerBuffer);

		gl.glBlendFunc(GL10.GL_DST_COLOR, GL10.GL_ZERO);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID[1]);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

		gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID[2]);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

		gl.glDisable(GL10.GL_BLEND);
		gl.glEnable(GL10.GL_DEPTH_TEST);
		
		update();
	}
	
	private boolean loadTextures(GL10 gl) {
		int res[] = {R.drawable.nehe_checker, R.drawable.nehe_rockchecker,
				R.drawable.nehe_zeus};
		
		mTextureID = new int[3];
		gl.glGenTextures(3, mTextureID, 0);

		for(int i=0; i<3; i++){
			Bitmap bmp = getTextureFromBitmapResource(mContext, res[i]);

			gl.glBindTexture(GL10.GL_TEXTURE_2D, mTextureID[i]);
			
			GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
			
			gl.glTexParameterf(GL10.GL_TEXTURE_2D, 
					GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
			gl.glTexParameterf(GL10.GL_TEXTURE_2D,
					GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
				
			bmp.recycle();
		}

		return true;
	}
	
	private void update(){
		wrapValue += 0.1f;

		if (wrapValue > 1.0f)
			wrapValue = 0.0f;

		checkerCoords[0] = 3.0f + wrapValue;
		checkerCoords[2] = 3.0f + wrapValue;
		checkerCoords[4] = 0.0f + wrapValue;
		checkerCoords[6] = 0.0f + wrapValue;
		
		mCheckerBuffer = createFloatBuffer(checkerCoords);
	}
	
	@Override
	public boolean handleTouchEvent(MotionEvent event) {
		return false;
	}	
}